package com.xiaomi.ad.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.miui.zeus.mimo.sdk.ad.AdWorkerFactory;
import com.miui.zeus.mimo.sdk.ad.IAdWorker;
import com.miui.zeus.mimo.sdk.listener.MimoAdListener;
import com.xiaomi.ad.common.pojo.AdType;
import com.xiaomi.ad.mimo.demo.R;

public class StimulateAdActivity extends Activity {
    private static final String TAG = "StimulateAdActivity";

    private static final String POSITION_ID = "6d089fcf31523ea73ca94138571ed31e";

    private IAdWorker mWorker;
    private boolean mHasStimulate;
    private int mAdSize;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stimulate_ad_layout);
        final ViewGroup container1 = findViewById(R.id.container1);
        final ViewGroup container2 = findViewById(R.id.container2);

        findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    mWorker = AdWorkerFactory.getAdWorker(getApplicationContext(), null, new MimoAdListener() {
                        @Override
                        public void onAdPresent() {
                            Log.e(TAG, "onAdPresent");
                        }

                        @Override
                        public void onAdClick() {
                            Log.e(TAG, "onAdClick");
                        }

                        @Override
                        public void onAdDismissed() {
                            Log.e(TAG, "onAdDismissed");
                        }

                        @Override
                        public void onAdFailed(String s) {
                            Log.e(TAG, "onAdFailed : " + s);
                        }

                        @Override
                        public void onAdLoaded(int i) {
                            Log.e(TAG, "onAdLoaded : " + i);
                            mAdSize = i;
                            findViewById(R.id.show).setEnabled(true);
                        }

                        @Override
                        public void onStimulateSuccess() {
                            if (mHasStimulate == false) {
                                mHasStimulate = true;
                                Log.e(TAG, "onStimulateSuccess");
                            }
                        }
                    }, AdType.AD_STIMULATE_DOWNLOAD);
                    mWorker.load(POSITION_ID);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (mAdSize >= 1) {
                        container1.addView(mWorker.updateAdView(null, 0));
                    }
                    if (mAdSize > 1) {
                        container2.addView(mWorker.updateAdView(null, 1));
                    }
                    findViewById(R.id.show).setEnabled(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            mWorker.recycle();
        } catch (Exception e) {

        }
    }

    private void recycle() {
        try {
            mWorker.recycle();
        } catch (Exception e) {
        }
    }
}
